package com.sghd.candy.module.account.manager;

import com.sghd.common.ramcache.anno.Inject;
import com.sghd.common.ramcache.orm.Querier;
import com.sghd.common.ramcache.service.EntityCacheService;
import com.sghd.common.utils.id.account.AccountIdGenerator;
import com.sghd.pojo.candy.account.AccountState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;

/**
 * @author frank
 * @date 2018-10-17 9:50
 * @desc
 **/
@Component
public class AccountManager {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Inject
    private EntityCacheService<Long, Account> accountCache;

    @Autowired
    private Querier querier;
    @Value("${platformId}")
    private short operator;
    @Value("${area}")
    private short area;
    private AccountIdGenerator generator;

    @PostConstruct
    void initialize() {
        long[] limits = AccountIdGenerator.getLimits(operator, area);
        Long max = querier.unique(Account.class, Long.class, Account.MAX_ID, limits[0], limits[1]);
        generator = new AccountIdGenerator(operator, area, max);
    }

    public Account load(long id) {
        return accountCache.load(id);
    }

    public Account create(String uid) {
        //生成id
        long id = generator.getNext();
        return accountCache.create(id, builder -> Account.valueOf(id, uid)
        );
    }

    public void login(Account account, String ip, boolean guest, int serverId) {
        account.login(new Date(), ip, guest, serverId);
        if (logger.isDebugEnabled()) {
            logger.debug("+++ 玩家[{}]登入[{}], 连续在线[{}]...", account.getId(),serverId, account.getContinuousDays());
        }
    }

    public void setAccountCache(EntityCacheService<Long, Account> accountCache) {
        this.accountCache = accountCache;
    }

    public Account loadByUid(String uid) {
        return querier.unique(Account.class, Account.ACCOUNT_BY_UID, uid);
    }

    public Account find(String uid) {
        Set<Account> results = accountCache.getFinder().find(account -> !account.getUid().equals(uid));
        if (results.isEmpty()) {
            return null;
        }
        return new ArrayList<>(results).get(0);
    }

    public void addState(Account account, AccountState state) {
        account.addState(state);
    }

    public void removeState(Account account, AccountState block) {
        account.removeState(block);
    }

    /**
     * 执行登出
     * @param account
     * @return
     */
    public void logout(Account account) {
        if (account != null) {
            account.logout(new Date());
            if (logger.isDebugEnabled()) {
                logger.debug("--- 玩家[{}]退出, 连续在线[{}]...", account.getId(), account.getContinuousDays());
            }
        }
    }
}
